package com.magicodex.bookkeeping.biz.accounting.event;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.magicodex.bookkeeping.R;
import com.magicodex.bookkeeping.biz.accounting.dto.AccountingDTO;
import com.magicodex.bookkeeping.biz.accounting.util.AccountingDataUtil;
import com.magicodex.bookkeeping.biz.common.util.CheckUtil;
import com.magicodex.bookkeeping.biz.common.util.StringUtil;
import com.magicodex.bookkeeping.biz.common.util.TextViewUtil;
import com.magicodex.bookkeeping.ui.accounting.AccountingListViewAdapter;

import java.math.BigDecimal;
import java.time.ZonedDateTime;

/**
 * @author mh.z
 */
public class EditFabClickListener implements View.OnClickListener {
    private Activity activity;
    private AccountingListViewAdapter adapter;

    public EditFabClickListener(Activity activity, AccountingListViewAdapter adapter) {
        this.activity = activity;
        this.adapter = adapter;
    }

    @Override
    public void onClick(View view) {
        int position = adapter.getLastClickPosition();

        if (!(position >= 0 && position < adapter.getCount())) {
            return;
        }

        LayoutInflater inflater = activity.getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.dialog_accounting, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle(R.string.dialog_title_edit_account);
        builder.setView(dialogView);
        builder.setPositiveButton(R.string.button_save, createPositiveButtonClickListener(dialogView));
        builder.setNegativeButton(R.string.button_cancel, createNegativeButtonClickListener());
        // 创建对话框
        AlertDialog dialog = builder.create();

        AccountingDTO record = (AccountingDTO) adapter.getItem(position);
        // 账户
        String account = record.getAccount();
        TextView accountView = dialogView.findViewById(R.id.text_account);
        TextViewUtil.setString(accountView, account);
        // 金额
        BigDecimal money = record.getMoney();
        TextView moneyView = dialogView.findViewById(R.id.text_money);
        TextViewUtil.setBigDecimalWithDefaultFmt(moneyView, money);

        dialog.show();
    }

    /**
     * 点击确定按钮的事件监听器
     *
     * @param dialogView
     * @return
     */
    protected DialogInterface.OnClickListener createPositiveButtonClickListener(View dialogView) {
        CheckUtil.notNull(dialogView, "dialogView null");

        return (dialog, which) -> {
            TextView accountView = dialogView.findViewById(R.id.text_account);
            TextView moneyView = dialogView.findViewById(R.id.text_money);

            int position = adapter.getLastClickPosition();
            AccountingDTO record = (AccountingDTO) adapter.getItem(position);
            // 账户
            String account = TextViewUtil.getString(accountView);
            account = StringUtil.trim(account);
            record.setAccount(account);
            // 金额
            BigDecimal money = TextViewUtil.getBigDecimalWithDefaultFmt(moneyView);
            record.setMoney(money);

            // 最后和核对日期
            if (money != null) {
                record.setLastCheckedDate(ZonedDateTime.now());
            } else {
                record.setLastCheckedDate(null);
            }

            // 数据变更后处理
            AccountingDataUtil.notifyDataSetChanged(activity, adapter);
        };
    }

    /**
     * 点击取消按钮的事件监听器
     *
     * @return
     */
    protected DialogInterface.OnClickListener createNegativeButtonClickListener() {
        return (dialog, which) -> {
            //
        };
    }

}
